<template>
  <common-select
    :options="partOptions"
    :defaultValue="defaultValue"
    @changeoption="changeOption"
    :allow-clear="true"
    :show-search="true"
  ></common-select>
</template>
<script>
import CommonSelect from '@/components/SelfComponents/CommonSelect.vue'
import ecuApi from '@/api/modules/system/ecuMana'
export default {
  props: {
    // 搜索条件
    query: {
      type: Object,
      default () {
        return {
          projectIds: [],
          modelIds: []
        }
      }
    },
    // 是否需要全部选项
    hasAll: {
      type: Boolean,
      default: true
    },
    defaultValue: {
      type: [String, Array, undefined, Object],
      default () {
        return undefined
      }
    }
  },
  components: {
    CommonSelect
  },
  computed: {},
  data () {
    return {
      defaultOption: '',
      partOptions: []
    }
  },
  model: {
    prop: "defaultValue", //绑定的值，通过父组件传递
    event: "changeoption" //自定义事件名
  },
  watch: {
    query: {
      handler () {
        this.getData()
      },
      immediate: true
    }
  },
  created () {

  },
  methods: {
    getData () {
      ecuApi.getAllEcu(this.query).then((res) => {
        const arr = this.hasAll ? [
          {
            id: '',
            name: this.$t('all') + this.$t('lingjian')
          }
        ] : []
        this.partOptions = arr.concat(res.data)
        if (!this.defaultOption && this.partOptions.length) {
          this.defaultOption = this.partOptions[0].id
        }
      })
    },
    changeOption (val) {
      this.$emit('changeoption', val)
    }
  }
}
</script>
